package com.demo2do.fms.admin.service.supplier.impl;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.fms.admin.dao.authority.BranchDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalResultDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalResultItemDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalTemplateDao;
import com.demo2do.fms.admin.dao.supplier.AppraisalTemplateItemDao;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.DataServiceImpl;
import com.demo2do.fms.admin.service.supplier.AppraisalTemplateService;
import com.demo2do.fms.entity.appraisal.Appraisal;
import com.demo2do.fms.entity.appraisal.AppraisalResult;
import com.demo2do.fms.entity.appraisal.AppraisalResultItem;
import com.demo2do.fms.entity.appraisal.AppraisalTemplate;
import com.demo2do.fms.entity.appraisal.AppraisalTemplateItem;
import com.demo2do.fms.entity.authority.Branch;
import com.demo2do.fms.entity.supplier.common.Supplier;

/**
 * 
 * @author houtao
 * 
 */
@Service("appraisalTemplateService")
public class AppraisalTemplateServiceImpl extends DataServiceImpl<AppraisalTemplate> implements AppraisalTemplateService {
    
    @Dao
    private AppraisalTemplateDao appraisalTemplateDao;
    
    @Dao
    private AppraisalTemplateItemDao appraisalTemplateItemDao;

    @Dao
    private BranchDao branchDao;
    
    @Dao
    private AppraisalDao appraisalDao;
    
    @Dao
    private AppraisalResultDao appraisalResultDao;
    
    @Dao
    private AppraisalResultItemDao appraisalResultItemDao;
    
	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#count(com.demo2do.fms.admin.entity.security.Login)
	 */
	public int count(Login login) {
	    
	    if (login.isSuper() || login.isAdminUser()) {
	        
	        return appraisalTemplateDao.count(true);
	        
	    } else if (login.isBranchUser()) {
	        
	        Branch branch = branchDao.load(login.getBranch());
	        return appraisalTemplateDao.count(branch);
	        
	    } else if (login.isSponsorUser() || login.isFmsUser()) {
	        
	        Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
	        if (supplier.isSecond()) {
	            return 0;
	        }
	        return appraisalTemplateDao.count(supplier);
	        
	    } else {
	        return 0;
	    }

	}

    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#list(com.demo2do.fms.admin.entity.security.Login)
     */
    public List<AppraisalTemplate> list(Login login) {
        
        if (login.isSuper() || login.isAdminUser()) {
            
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(true);
            return templates;
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(branch);
            return templates;
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(supplier);
            return templates;
            
        } else {
            return Collections.emptyList();
        }
        
    }
	
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#list(com.demo2do.fms.admin.entity.security.Login, com.demo2do.core.entity.page.Page)
     */
    public List<AppraisalTemplate> list(Login login, Page page) {
        
        page.calculate();
        
        if (login.isSuper() || login.isAdminUser()) {
            
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(true, page.getBeginIndex(), page.getEveryPage());
            return templates;
            
        } else if (login.isBranchUser()) {
            
            Branch branch = branchDao.load(login.getBranch());
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(branch, page.getBeginIndex(), page.getEveryPage());
            return templates;
            
        } else if (login.isSponsorUser() || login.isFmsUser()) {
            
            Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
            List<AppraisalTemplate> templates = appraisalTemplateDao.list(supplier, page.getBeginIndex(), page.getEveryPage());
            return templates;
            
        } else {
            return Collections.emptyList();
        }
        
    }

	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#create(com.demo2do.fms.entity.appraisal.AppraisalTemplate, com.demo2do.fms.admin.entity.security.Login)
	 */
	public void create(AppraisalTemplate template, Login login) {
	    
	    Long operator = login.getId();
	    
	    // 1. valid unique
	    AppraisalTemplate existTemplate = appraisalTemplateDao.getByName(template.getName());
	    if (existTemplate != null) {
	        throw BusinessException.newInstance("500", "已存在同名绩效考核模板");
	    }
	    
	    // 2. save template
	    if (login.isAdminUser() || login.isSuper()) {
	        
	        genericDaoSupport.save(template.initialize(true, operator));
	        
	    } else if (login.isBranchUser()) {
	        
	        Branch branch = branchDao.load(login.getBranch());
	        genericDaoSupport.save(template.initialize(branch, operator));
	        
	    } else if (login.isSponsorUser() || login.isFmsUser()) {
	        
	        Supplier supplier = genericDaoSupport.load(Supplier.class, login.getSupplierId());
	        if (supplier.isSecond()) {
                throw BusinessException.newInstance("500", "权限不足，无法创建");
            }
	        genericDaoSupport.save(template.initialize(supplier, operator));
	        
	    } else {
	        throw BusinessException.newInstance("500", "权限不足，无法创建");
	    }
	    
	}
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#edit(com.demo2do.fms.entity.appraisal.AppraisalTemplate, com.demo2do.fms.entity.appraisal.AppraisalTemplate, java.lang.Long)
	 */
	public void edit(AppraisalTemplate oldTemplate, AppraisalTemplate changeTemplate, Long operator) {

	    // 1. valid whether can edit
        int totalAppraisal = appraisalDao.count(oldTemplate);
        if (totalAppraisal > 0) {
            throw BusinessException.newInstance("500", "此标准已生成考核，无法变更");
        }
        
        // 2. edit
        genericDaoSupport.update(oldTemplate.edit(changeTemplate, operator));
	    
	}
	
	/* (non-Javadoc)
	 * @see com.demo2do.fms.admin.service.supplier.AppraisalTemplateService#delete(com.demo2do.fms.entity.appraisal.AppraisalTemplate, java.lang.Long)
	 */
	public void delete(AppraisalTemplate template, Long operator) {
	    
	    // 1. delete appraisal
	    List<Appraisal> appraisals = appraisalDao.list(template);
	    for (Appraisal appraisal : appraisals) {
	        
	        // 2. delete appraisal_result and items
	        List<AppraisalResult> appraisalResults = appraisalResultDao.list(appraisal);
	        for (AppraisalResult appraisalResult : appraisalResults) {
	            
	            List<AppraisalResultItem> itemResults = appraisalResultItemDao.list(appraisalResult);
	            for (AppraisalResultItem itemResult : itemResults) {
	                genericDaoSupport.update(itemResult.delete(operator));
	            }
	            
	            genericDaoSupport.update(appraisalResult.delete(operator));
	            
	        }

	        // delete appraisal
	        genericDaoSupport.update(appraisal.delete(operator));
	        
	    }
	    
	    // 3. delete appraisal_template_items
        List<AppraisalTemplateItem> templateItems = appraisalTemplateItemDao.list(template);
        for (AppraisalTemplateItem templateItem : templateItems) {
            genericDaoSupport.update(templateItem.delete(operator));
        }
	    genericDaoSupport.update(template.delete(operator));
	    
	}
	
}